package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author lynn
 * @since 2025-03-12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> treeSelect(Integer id) {
        //根据父id查询所有的子节点
        List<Category> categoryList = categoryMapper.selectList(new LambdaUpdateWrapper<Category>().eq(Category::getParentId, id));
        if (categoryList != null) {
            for (Category category : categoryList) {
                //查询是否有子节点
                Long categoryId = category.getId();//自己的id
                //
                Long childCount = categoryMapper.selectCount(new LambdaUpdateWrapper<Category>().eq(Category::getParentId, categoryId));
                category.setHasChildren(childCount > 0);
            }
        }
        return categoryList;
    }


    /**
     * 获取一级分类CategoryVo列表
     * @return
     */
    @Override
    public List<CategoryVo> getLevelOneCategory() {
        List<Category> categoryList = this.treeSelect(0);
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for (Category category : categoryList){
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            categoryVoList.add(categoryVo);
        }
        return categoryVoList;
    }

    //获取分类树结构
    @Override
    public List<CategoryVo> tree() {
        //查询所有分类
        List<Category> allCategoryList = categoryMapper.selectList(null);
        //转换为vo
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        //转换为树形结构
        return buildTree(categoryVoList);
    }

    //使用递归方法构建分类树
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //创建树形结构
        List<CategoryVo> trees = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryVoList) {
            //判断是否是根节点
            if (categoryVo.getParentId().longValue() == 0) {
                //查找当前节点的子节点，添加到树形结构中
                categoryVo = findAndAddChildren(categoryVo, categoryVoList);
                trees.add(categoryVo);
            }
        }
        return trees;
    }

    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        //遍历所有节点
        for (CategoryVo node : categoryVoList) {
            //判断是否是categoryVo的子节点
            if(node.getParentId().longValue() == categoryVo.getId().longValue()) {
                //在categoryVoList中找到categoryVo的子节点，
                CategoryVo children = findAndAddChildren(node, categoryVoList);
                //并组装到categoryVo中
                categoryVo.getChildren().add(children);
            }
        }
        return categoryVo;
    }
}
